package cn.qinhao.pethome.org.controller;

import cn.qinhao.pethome.basic.until.AjaxResult;
import cn.qinhao.pethome.basic.until.PageList;
import cn.qinhao.pethome.org.domain.User;
import cn.qinhao.pethome.org.query.UserQuery;
import cn.qinhao.pethome.org.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "新增或修改操作" )
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功");
        } catch (Exception e) {
            return AjaxResult .me() .setSuccess(false ).setMessage("保存失败");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功");
        } catch (Exception e) {
            return AjaxResult .me() .setSuccess(false ).setMessage("删除失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public User get(@PathVariable("id")Long id)
    {
        return userService.selectOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<User> list(){

        return userService.selectAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<User> queryPage(@RequestBody UserQuery query)
    {
        return userService.pageQuery(query);
    }
}
